var platform;
// 仅 Android 6 与 iOS 11 以上版本支持
export function checkPlatform() {
	return new Promise((resolve, reject) => {
		// 检测手机型号
		wx.getSystemInfo({
			success: function(res) {
				var system = "";
				console.log(res.system);
				if (res.platform == "android") system = parseInt(res.system.substr(8));
				if (res.platform == "ios") system = parseInt(res.system.substr(4));
				if (res.platform == "android" && system < 6) {
					wx.showToast({
						title: "手机系统不支持自动连接wifi",
						icon: "none",
						duration: 24 * 60 * 60 * 1000,
					});
					reject();
				}
				if (res.platform == "ios" && system < 11) {
					wx.showToast({
						title: "手机系统不支持自动连接wifi",
						icon: "none",
						duration: 24 * 60 * 60 * 1000,
					});
					reject();
				}

				platform = res.platform;
				resolve({
					platform,
					system
				});
			},
			fail: function(res) {
				reject("请求失败");
				wx.showToast({
					title: "请求失败",
					icon: "none",
					duration: 3000,
				});
			},
		});
	});
}

/*
 *初始化wifi模块
 */
export function initWifi() {
	return new Promise((resolve, reject) => {
		wx.startWifi({
			success(res) {
				console.log(res, 55555);
				resolve(res);
			},
			fail(err) {
				// uni.hideLoading();
				reject(err);
				uni.showToast({
					title: "wifi初始化失败",
					icon: "error",
					duration: 4000,
				});
			},
		});
	});
}

/*
 *连接wifi
 */
export function connectWifi(wifiInfo, maunal) {
	return new Promise((resolve, reject) => {
		// 2-连接wifi
		wx.connectWifi({
			...wifiInfo,
			maunal: maunal ? true : false,
			async success(con_res) {
				// 3-获取已连接的wifi信息，解决部分手机连接wifi失败仍然回调成功的情况
				try {
					const wifi = await getConnectedWifi();
					if (con_res.wifiMsg) {
						reject("wifi已经连接");
						return;
					}
					if (wifi.wifi.SSID == wifiInfo.SSID) {
						resolve(wifi);
					} else {
						reject(wifi);
					}
				} catch (err) {
					reject(err);
				}
			},
			fail(err) {
				if (getErrorMsg(err) == "wifi连接失败") {
					reject(err);
				}
				reject(getErrorMsg(err));
			},
		});
	});
}
/*
 *获取已连接wifi
 */
export function getConnectedWifi() {
	return new Promise((resolve, reject) => {
		wx.getConnectedWifi({
			success(wifi) {
				resolve(wifi);
				console.log(wifi, "获取已连接wifi成功");
			},
			fail(err) {
				if (
					err.errMsg == "getConnectedWifi:fail currentWifi is null" ||
					err.errMsg == "getConnectedWifi:fail:wifi is disable"
				) {
					uni.showToast({
						title: "此设备没有连接WiFi",
						icon: "none",
						duration: 4000,
					});
				}
				reject(err);
				console.log(err, "获取已连接wifi失败");
			},
		});
	});
}

function getErrorMsg(err) {
	let msg = "";
	switch (err.errCode) {
		case 12005:
			msg = "未打开 Wi-Fi 开关";
			break;
		case 12007:
			msg = "用户拒绝授权链接 Wi-Fi";
			break;
		case 12013:
			msg = "系统保存的 Wi-Fi 配置过期，建议忘记 Wi-Fi 后重试";
			break;
		case 12002:
			msg = "wifi密码错误,请联系装维人员..";
			break;
		default:
			msg = "wifi连接失败";
			break;
	}
	return msg;
}
export default {
	checkPlatform, // 检测平台是否支持
	initWifi,
	connectWifi,
	getConnectedWifi,
};